package service

import (
	"background/app/example/dao"
	"background/app/system/example/internal/define"
	"background/library/response"
	toolutilv4 "background/library/toolutil/v4"
	"fmt"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/os/glog"
	"strings"
	"time"
)

var User = userService{}

type userService struct{}

/*
	//日志打印 glog.Error("XXXXXX Parse error:", req)
*/

func (c *userService) UserSave(r *ghttp.Request) (interface{}, error) {
	var req *define.UserSaveReq
	if err := r.Parse(&req); err != nil {
		response.JsonExit(r, response.ClientError, err.Error())
	}

	dbModel := dao.ExampleUser.Where("")
	id, err := dbModel.InsertAndGetId(req)
	if err != nil {
		glog.Error("UserSave Parse error:", req, err.Error())
		response.JsonExit(r, response.ServerSqlError, err.Error())
	}

	return id, nil
}

func (c *userService) UserDel(r *ghttp.Request) (interface{}, error) {
	var req *define.UserDelReq
	if err := r.Parse(&req); err != nil {
		response.JsonExit(r, response.ClientError, err.Error())
	}

	dbModel := dao.ExampleUser.Where("")
	if req.Id == 0 {
		response.JsonExit(r, response.ClientError, "id 不能为空")
	} else {
		dbModel = dbModel.Where("id = ?", req.Id)
	}
	row, err := dbModel.Delete()
	fmt.Println(row.RowsAffected()) //受影响的行数
	if err != nil {
		glog.Error("UserDel Parse error:", req, err.Error())
		response.JsonExit(r, response.ServerSqlError, err.Error())
	}

	return req.Id, nil
}

func (c *userService) UserUpd(r *ghttp.Request) (interface{}, error) {
	var req *define.UserUpdReq
	if err := r.Parse(&req); err != nil {
		response.JsonExit(r, response.ClientError, err.Error())
	}
	dbModel := dao.ExampleUser.Where("")
	if req.Id == 0 {
		response.JsonExit(r, response.ClientError, "id 不能为空")
	} else {
		dbModel = dbModel.Where("id = ?", req.Id)
	}

	_, err := dbModel.OmitEmptyData().Update(req) // OmitEmptyData 零值的字段不更新
	if err != nil {
		glog.Error("UserUpd Parse error:", req, err.Error())
		response.JsonExit(r, response.ServerSqlError, err.Error())
	}

	return req.Id, nil
}

func (c *userService) UserList(r *ghttp.Request) (interface{}, error) {
	var req *define.UserListReq
	if err := r.Parse(&req); err != nil {
		response.JsonExit(r, response.ClientError, err.Error())
	}

	dbModel := dao.ExampleUser.Where("")
	if req.Id != 0 {
		dbModel = dbModel.Where("id = ?", req.Id)
	}
	if req.Name != "" {
		dbModel = dbModel.Where("name like ?", "%"+req.Name+"%")
	}
	if req.AgeStr != "" {
		req.AgeList = toolutilv4.ToolSlice.ConvertStringSliceToIntSlice(strings.Split(req.AgeStr, ","))
		if len(req.AgeList) > 0 {
			dbModel = dbModel.Where("age in (?)", req.AgeList)
		}
	}

	result, err := dbModel.Fields("id, name, content, created_at").All()
	if err != nil {
		glog.Error("UserList Parse error:", req, err.Error())
		response.JsonExit(r, response.ServerSqlError, err.Error())
	}
	list := result.List()

	for _, v := range list {
		delete(v, "id") //移除字段
	}
	return list, nil
}

func (c *userService) V2Upd(r *ghttp.Request) (interface{}, error) {
	var req *define.V2UpdReq
	if err := r.Parse(&req); err != nil {
		response.JsonExit(r, response.ClientError, err.Error())
	}
	dbModel := dao.ExampleUser.Where("")
	if req.Name == "" || req.Content == "" {
		response.JsonExit(r, response.ClientError, "参数不能为空")
	}

	tx, _ := g.DB("example").Begin()
	dbModel.TX(tx).Where(g.Map{"id": 1}).LockUpdate().All() // 加悲观锁
	time.Sleep(time.Second * 60)
	dbModel.TX(tx).Data(g.Map{"content": time.Now().String()}).Where(g.Map{"id": 1}).Update()
	tx.Commit()

	return req.Id, nil
}

func (c *userService) V2Cfg(r *ghttp.Request) (interface{}, error) {
	fmt.Println(g.Cfg().GetString("gfcli.build.name"))

	return 1, nil
}

func (c *userService) V2Log(r *ghttp.Request) (interface{}, error) {

	glog.Async().Info("V2Cfg Parse error:", 1)
	return 1, nil
}
